﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace CMTVision.Widgets.Framework
{
    public class BaseTemplate : System.Web.UI.UserControl, CMTVision.Widgets.Framework.ITemplate
    {
        #region Ctor

        public BaseTemplate()
            : base()
        {
            IsInEditMode = false; 
        }

        #endregion

        #region Events & Handler
          

        #endregion

        #region Implement ITemplate interface

        public long TemplateContainerID
        {
            get;
            set;
        }

        public long TemplateID
        {
            get;
            set;
        }

        /// <summary>
        /// 
        /// </summary>
        public bool IsInEditMode
        {
            get;
            set;
        }
        
        public void LoadTemplateColumns()
        {
            if (TemplateID > 0)
            {
                List<string> allColumnIdList = new List<string>();
                using (Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
                {
                    var columns = (from t in entity.Templates.Include("TemplateType") where t.ID == TemplateID select t.TemplateType.Columns).FirstOrDefault();
                    if (!String.IsNullOrEmpty(columns))
                    {
                        allColumnIdList = columns.Split(",".ToArray()).ToList();
                    }
                }
                allColumnIdList.ForEach(columnId =>
                {
                    var columnControl = FindControl(columnId) as CMTVision.Widgets.Framework.ITemplateColumn;
                    if (columnControl != null)
                    {
                        columnControl.ColumnID = columnId;
                        columnControl.TemplateID = TemplateID;
                        columnControl.IsInEditMode = IsInEditMode;
                        columnControl.LoadWidgets();
                    }
                });
            }
        }

        public virtual void Refresh()
        {
            if (TemplateID > 0)
            {
                var allColumnIdList = new List<string>();
                using (Model.CMTWidgetsEntities entity = CMTVision.Common.EntitiesManager.LoadWidgetEntity())
                {
                    allColumnIdList = (from w in entity.Widgets where w.Template.ID == TemplateID select w.ColumnID).ToList();                    
                }
                allColumnIdList.ForEach(columnId =>
                {
                    var columnControl = FindControl(columnId) as CMTVision.Widgets.Framework.ITemplateColumn;
                    if (columnControl != null)
                    {
                        columnControl.Refresh();
                    }
                });
            }                        
        }

        #endregion

    }
}